package com.ukao.cashregister.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.listener.MyTextWatcher;
import com.ukao.cashregister.listener.OnDialogSureClickListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 修改名字
 * Created by ukaodev on 2017/6/2.
 */

public class SelectNumDialogFragment extends BaseDialogFragment {

    @BindView(R.id.edit_name_et)
    EditText mNameEt;
    @BindView(R.id.edit_name_cancel)
    Button meCancel;
    @BindView(R.id.edit_name_submit)
    Button mSubmit;
    Unbinder unbinder;
    @BindView(R.id.title)
    TextView mtitle;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.unitText)
    TextView unitText;
    @BindView(R.id.txt_modification)
    TextView txtModification;


    public static SelectNumDialogFragment newInstance(String unitTitle, String unitPice,String  mDialogTitle,boolean isModificationVisibility,int onClickId) {
        SelectNumDialogFragment fragment = new SelectNumDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, unitTitle);
        args.putString(ARG_PARAM2, unitPice);
        args.putString(ARG_PARAM3, mDialogTitle);
        args.putInt(ARG_PARAM4, onClickId);
        args.putBoolean(ARG_PARAM5, isModificationVisibility);
        fragment.setArguments(args);
        return fragment;
    }

    private  String    unitTitle;
    private  String    unitPice;
    private  String   mDialogTitle;
    private  boolean isModificationVisibility=false;
    private int onClickId;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            unitTitle=getArguments().getString(ARG_PARAM1);
            unitPice=getArguments().getString(ARG_PARAM2);
            mDialogTitle=getArguments().getString(ARG_PARAM3);
            onClickId=getArguments().getInt(ARG_PARAM4,0);
            isModificationVisibility=getArguments().getBoolean(ARG_PARAM5, false);
        }

    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_fragment_select_num_layout, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }




    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unitText.setText(unitTitle);
        price.setText(unitPice);
        txtModification.setVisibility(isModificationVisibility?View.VISIBLE:View.INVISIBLE);
        mtitle.setText(mDialogTitle);
        initListener();

    }

    @Override
    protected void initListener() {
        super.initListener();
        mNameEt.addTextChangedListener(mTextWatcher);

    }

    private TextWatcher  mTextWatcher=new MyTextWatcher(){
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            super.onTextChanged(s, start, before, count);
            if (s.toString().contains(".")) {
                if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                    s = s.toString().subSequence(0,
                            s.toString().indexOf(".") + 3);
                    mNameEt.setText(s);
                    mNameEt.setSelection(s.length());
                }
            }
            if (s.toString().trim().substring(0).equals(".")) {
                s = "0" + s;
                mNameEt.setText(s);
                mNameEt.setSelection(2);
            }

            if (s.toString().startsWith("0")
                    && s.toString().trim().length() > 1) {
                if (!s.toString().substring(1, 2).equals(".")) {
                    mNameEt.setText(s.subSequence(0, 1));
                    mNameEt.setSelection(1);
                    return;
                }
            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.edit_name_cancel, R.id.edit_name_submit,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case  R.id.close_btn:

                dismiss();
                break;

            case R.id.edit_name_cancel:

                dismiss();
                break;
            case R.id.edit_name_submit:
                String temMessage = mNameEt.getText().toString().trim();
                if (temMessage.isEmpty()) {

                    return;
                }
                hideKeyboard();
                if (mOnDialogSureClickListener != null)
                    mOnDialogSureClickListener.onSureItemClick(temMessage);

                dismiss();
                break;
        }
    }



    public static Builder createBuilder(
            FragmentManager fragmentManager) {
        return new Builder(fragmentManager);
    }

    public static   class   Builder{




        private  String    unitTitle;
        private  String    unitPice;
        private  String   mDialogTitle;
        private  boolean isModificationVisibility=false;
        private  int view;
        private final FragmentManager mFragmentManager;

        private OnDialogSureClickListener mListener;
        public   Builder(FragmentManager fragmentManager){
            mFragmentManager=fragmentManager;

        }


        public Builder setUnitTitle(String unitTitle) {
            this.unitTitle = unitTitle;
            return  this;
        }

        public Builder setUnitPice(String unitPice) {
            this.unitPice = unitPice;
            return  this;
        }

        public Builder setmDialogTitle(String mDialogTitle) {
            this.mDialogTitle = mDialogTitle;
            return  this;
        }

        public Builder setModificationVisibility(boolean modificationVisibility) {
            isModificationVisibility = modificationVisibility;
            return  this;
        }

        public Builder setListener(OnDialogSureClickListener listener) {
            this.mListener = listener;
            return this;
        }

        public Builder setView(int view) {
            this.view = view;
            return this;

        }

        public   SelectNumDialogFragment  show(){
            SelectNumDialogFragment mSelectNumDialog =SelectNumDialogFragment.newInstance(unitTitle,unitPice,mDialogTitle,isModificationVisibility,view);
            mSelectNumDialog.setOnSureClickListener(mListener);
            mSelectNumDialog.show(mFragmentManager,mSelectNumDialog.getClass().getName());
            return  mSelectNumDialog;
        }


    }




}
